package com.quanyan.stadium.service.commonComponent.impl;

import com.quanyan.comment.enums.TaskEnum;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.stadium.component.mq.MqProducer;
import com.quanyan.stadium.service.commonComponent.DatesportMqService;
import com.quanyan.user.mq.UserTaskMsg;
import com.quanyan.user.serviceFacade.UserTaskServiceFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Zheng.Ke
 * Date 2016/7/18.
 */
@Service("datesportMqService")
public class DatesportMqServiceImpl implements DatesportMqService {

    private static final Logger logger = LoggerFactory.getLogger(DatesportMqServiceImpl.class);

    @Autowired
    private MqProducer mqProducer;
    @Autowired
    private UserTaskServiceFacade userTaskServiceFacade;

    @Override
    public void sendSportsWithMe(int uid) {
        UserTaskMsg userTaskMsg = new UserTaskMsg();
        userTaskMsg.setUid(uid);
        userTaskMsg.setTaskId(TaskEnum.TASK_ENUM__SPORTS_WITH_ME.getId());

        sendUserTaskMsg(userTaskMsg);
    }

    private void sendUserTaskMsg(UserTaskMsg userTaskMsg) {
        logger.info("开始发送消息到mq:{}",userTaskMsg.toString());
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setTopic(Constants.MQ_TOPIC_TASK_TOPIC);
        mqRequestMsg.setTag(Constants.MQ_TOPIC_TASK_TAG);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(userTaskMsg));
        mqProducer.syncSend(mqRequestMsg);
        logger.info("发送消息完毕==========》");
    }

    @Override
    public void sendIToSports(int uid) {
        UserTaskMsg userTaskMsg = new UserTaskMsg();
        userTaskMsg.setUid(uid);
        userTaskMsg.setTaskId(TaskEnum.TASK_ENUM__I_TO_SPORTS.getId());

        sendUserTaskMsg(userTaskMsg);
    }
}
